<?php
// +----------------------------------------------------------------------
// | HisiPHP框架[基于ThinkPHP5.1开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2016-2021 http://www.hisiphp.com
// +----------------------------------------------------------------------
// | HisiPHP承诺基础框架永久免费开源，您可用于学习和商用，但必须保留软件版权信息。
// +----------------------------------------------------------------------
// | Author: 橘子俊 <364666827@qq.com>，开发者QQ群：50304283
// +----------------------------------------------------------------------

namespace app\api\validate;

use app\common\model\User;
use think\facade\Log;
use think\Validate;

/**
 * 登录验证器
 * @package app\api\validate
 */
class LoginValidate extends Validate
{
    //定义验证规则
    protected $rule = [
        'type|登录类型'    => 'require|between:1,2|sceneType',
        'account|账号'    => 'require|scenePhone',
        'password|密码'   => 'require|min:8|scenePassword',
        'code|短信验证码'  => 'require|min:6|sceneCode',
    ];

    //定义验证提示
    protected $message = [
        'account.scenePhone'    => '手机号未注册',
        'password.scenePassword'=> '密码错误',
        'type.require'          => '请选择登录类型',
        'type.between'          => '只能选择短信注册或密码注册',
        'type.sceneType'        => '登录类型填写错误',
        'code.sceneCode'        => '短信验证码错误',
    ];


    public function sceneType($type)
    {
        switch ($type){
            case 1:
                //账号登录验证密码不验证短信验证码
                $this->append('password', ['require','min','scenePassword'])
                     ->remove('code',['require','min','sceneCode']);
                return true;
                case 2:
                //手机号登录不验证密码验证短信码
                $this->remove('password', ['require','min','scenePassword'])
                    ->append('code',['require','min','sceneCode']);
                return true;
            default:
                return false;
        }
    }


    /**
     *验证手机号
     */
    public function scenePhone($phone)
    {
        $where['username|mobile'] =  $phone;
        $token = User::where($where)->count();
        return (bool)$token;
    }

    public function scenePassword($password)
    {
        $data = request()->post();
        $account = isset($data['phone']) ? $data['phone'] : '';
        if (empty($account)){
            $account = isset($data['account']) ? $data['account'] : '';
        }
        if (empty($account)){
           return  false;
        }
        $where['username|mobile'] =  $account;
        //密码验证
        $getPassword = User::where($where)->value('password');
        if (!password_verify(md5($password), $getPassword)){
            return false;
        }
        return true;
    }

    /**
     *手机号登录验证码验证
     */
    public function sceneCode($code)
    {
        $account = request()->post('account');
        try {
            $redisCode = redis()->get('LoginCode_' . $account);
            redis()->del('LoginCode_' . $account);
            redis()->close();
        } catch (\RedisException $e) {
            Log::info('LoginCodeError_'.$e->getMessage());
        }
       return $code === $redisCode;
    }

    /**
     * 验证场景
     */
    protected $scene = [

    ];
}
